package com.example.shop.goods.dao;

import com.example.shop.goods.dto.GoodsDetailBrowseInfo;
import com.example.shop.goods.enums.ShelfState;
import com.example.shop.goods.typehandler.ShelfStateTypeHandler;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface GoodsDetailBrowseInfoDao {

    public static final String ALL_FIELDS_SQL = GoodsBrowseInfoDao.ALL_FIELDS_SQL +
            ",g.bad_comment_number, g.favorable_comment_number, g.medium_comment_number," +
            "g.goods_video,g.goods_introduction, g.shelf_time, g.shelf_state ";

    @Results(id = "GoodsDetailBrowseInfoResultMap",value = {
            @Result(column = "goods_id",property = "goodsId"),
            @Result(column = "goods_id",property = "goodsDescriptionPicture",
                    many = @Many(select="com.example.shop.mapper.GoodsDescriptionPictureMapper.selectPictureById")),
            @Result(column = "goods_id",property = "goodsDetailsPicture",
                    many = @Many(select = "com.example.shop.mapper.GoodsDetailsPictureMapper.selectPictureById")),
            @Result(property = "goodsStyleList",
                    many = @Many(resultMap = "com.example.shop.goods.dao.GoodsStyleDao.GoodsStyleDaoResultMap")),
            @Result(column = "goods_id",property = "evaluationList",
                    many = @Many(select = "com.example.shop.goods.dao.EvaluationBrowseInfoDao.selectByGoodsId")),
            @Result(column = "shelf_state",property = "shelfState",jdbcType = JdbcType.TINYINT,javaType = ShelfState.class,typeHandler = ShelfStateTypeHandler.class)
    })
    @Select("<script>" +
            "select " + ALL_FIELDS_SQL +
            "from " +
            "(goods g join r_goods_class gc on g.goods_id = gc.goods_id) " +
            "left join " +
            "(evaluation e join `user` u on e.user_id = u.user_id) " +
            "on g.goods_id = e.goods_id "+
            "where g.goods_id = ${goodsId}"+
            "</script>")
    GoodsDetailBrowseInfo selectOne(Long goodsId);

    @ResultMap("GoodsDetailBrowseInfoResultMap")
    @Select("<script>" +
            "select "  + ALL_FIELDS_SQL +
            "from " +
            "(goods g join r_goods_class gc on g.goods_id = gc.goods_id) " +
            "left join " +
            "(evaluation e join `user` u on e.user_id = u.user_id) " +
            "on g.goods_id = e.goods_id "+
            "limit 0,${pageNum*pageSize}"+
            "</script>")
    List<GoodsDetailBrowseInfo> selectPage(@Param("pageNum") int pageNum,@Param("pageSize") int pageSize);

}
